/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:assign-tracer
 * Create Time: 2016年2月19日 下午7:03:26
 */
package com.lenovo.lmrp.server.assigntracer.pusher;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.lenovo.lmrp.api.model.entity.MessageTemplatePO;
import com.lenovo.lmrp.api.model.view.EngineerVO;
import com.lenovo.lmrp.server.assigntracer.util.cache.AssignTracerCache;
import com.lenovo.lmrp.server.assigntracer.util.pusher.EmailSender;
import com.lenovo.lmrp.server.assigntracer.util.pusher.GeTuiSender;
import com.lenovo.lmrp.server.assigntracer.util.pusher.SMSSender;
import com.lenovo.lmrp.server.assigntracer.util.pusher.SenderUtil;
import com.lenovo.sdc.framework.util.AttributeBean;

/**
 * 
 * @author songyz
 * @version $Id: SendMessageException.java, v 0.1 2016年2月19日 下午7:03:26 songyz
 *          Exp $
 */
public class SendMessageException {

	@Before
	public void bf() {
		AttributeBean params = new AttributeBean();
		// 我个人邮箱
		params.addAttribute("email.smtpHost", "smtp.163.com");
		params.addAttribute("email.user", "songyinzeng0310@163.com");
		// params.addAttribute("email.passwd", "song66668888");
		params.addAttribute("email.passwd", "song6666888");// 有限账号有误
		params.addAttribute("email.auth", "true");

		// 短信账号信息
		params.addAttribute("short.host",
				"http://10.99.124.1/webservice/deliverMessage/SmsService.asmx?op=SendMessage");
		params.addAttribute("short.username", "gaoyi4");
		params.addAttribute("short.password", "gaoyi4");

		// 我自己的个推信息
		params.addAttribute("geTui.appId", "SKWE0oJYWh7yBgvj9ooed5");
		params.addAttribute("geTui.appKey", "uR8fa9QQnz9LOll5xhq2");
		params.addAttribute("geTui.master", "kGNyowLvqk7KBNnOQIdbF4");
		params.addAttribute("geTui.host", "http://sdk.open.api.igexin.com/serviceex");
		params.addAttribute("geTui.message.offLine", "true");
		params.addAttribute("geTui.message.offLineTime", "86400000");

		// params.addAttribute("http.proxy.ip", "10.99.236.2");
		// params.addAttribute("http.proxy.port", "8080");

		params.addAttribute("threadPool.corepoolsize", "4");
		params.addAttribute("threadPool.maxinumpoolsize", "4");
		params.addAttribute("threadPool.keepalivetime", "10");

		params.addAttribute("assigntracer.cache.partition", "assigntracer");
		params.addAttribute("assigntracer.cache.server", "app");
		params.addAttribute(" assigntracer.cache.maxitems", "10000");
		params.addAttribute("assigntracer.cache.maxttl", "10");

		new AssignTracerCache(params);
		SenderUtil.set(params);
		SenderUtil.set(new EmailSender(params));
		SenderUtil.set(new SMSSender(params));
		SenderUtil.set(new GeTuiSender(params));
	}

	/**
	 * 发送发送全部失败，重试三次
	 */
	@Test
	// 测试所有的发送失败
	public void testSendAllError() {
		EngineerVO engineerVO = new EngineerVO();
		engineerVO.setEmail("18518113715@163.com");
		engineerVO.setEngineerid("songyz");
		// engineerVO.setEngineerOnLineCids("fd3064257eedc0263f95297e3af896ef");
		engineerVO.setEngineerOnLineCids("fd3257eedc0263f95297e3af896ef");// 有误的cid
		engineerVO.setLoginname("songyz");
		// engineerVO.setPhone("18518113715");
		engineerVO.setPhone("185181137");

		List<MessageTemplatePO> messageTemplateList = new ArrayList<>();
		MessageTemplatePO messageTemplatePO = new MessageTemplatePO();
		messageTemplatePO.setMessagetype(7);
		messageTemplatePO.setSendmode(0);
		messageTemplateList.add(messageTemplatePO);

		messageTemplatePO = new MessageTemplatePO();
		messageTemplatePO.setMessagetype(7);
		messageTemplatePO.setSendmode(1);
		messageTemplateList.add(messageTemplatePO);

		messageTemplatePO = new MessageTemplatePO();
		messageTemplatePO.setMessagetype(7);
		messageTemplatePO.setSendmode(2);
		messageTemplateList.add(messageTemplatePO);

		SenderUtil.sendMessageCreateCache(engineerVO, messageTemplateList);
	}

	/**
	 * 短信发送成功，其他全部失败
	 */
	@Test
	// 测试所有的发送失败
	public void testSendError1() {
		EngineerVO engineerVO = new EngineerVO();
		engineerVO.setEmail("18518113715@163.com");
		engineerVO.setEngineerid("songyz");
		// 有误的cid
		engineerVO.setEngineerOnLineCids("fd3257eedc0263f95297e3af896ef");//
		engineerVO.setLoginname("songyz");
		engineerVO.setPhone("18518113715");

		List<MessageTemplatePO> messageTemplateList = new ArrayList<>();
		MessageTemplatePO messageTemplatePO = new MessageTemplatePO();
		messageTemplatePO.setMessagetype(7);
		messageTemplatePO.setSendmode(0);
		messageTemplateList.add(messageTemplatePO);

		messageTemplatePO = new MessageTemplatePO();
		messageTemplatePO.setMessagetype(7);
		messageTemplatePO.setSendmode(1);
		messageTemplateList.add(messageTemplatePO);

		messageTemplatePO = new MessageTemplatePO();
		messageTemplatePO.setMessagetype(7);
		messageTemplatePO.setSendmode(2);
		messageTemplateList.add(messageTemplatePO);

		SenderUtil.sendMessageCreateCache(engineerVO, messageTemplateList);
	}

	/**
	 * 个推发送成功，其他全部失败
	 */
	@Test
	// 测试所有的发送失败
	public void testSendError2() {
		EngineerVO engineerVO = new EngineerVO();
		engineerVO.setEmail("18518113715@163.com");
		engineerVO.setEngineerid("songyz");
		// 有误的cid
		engineerVO.setEngineerOnLineCids("fd3064257eedc0263f95297e3af896ef");//
		engineerVO.setLoginname("songyz");
		engineerVO.setPhone("118113715");

		List<MessageTemplatePO> messageTemplateList = new ArrayList<>();
		MessageTemplatePO messageTemplatePO = new MessageTemplatePO();
		messageTemplatePO.setMessagetype(7);
		messageTemplatePO.setSendmode(0);
		messageTemplateList.add(messageTemplatePO);

		messageTemplatePO = new MessageTemplatePO();
		messageTemplatePO.setMessagetype(7);
		messageTemplatePO.setSendmode(1);
		messageTemplateList.add(messageTemplatePO);

		messageTemplatePO = new MessageTemplatePO();
		messageTemplatePO.setMessagetype(7);
		messageTemplatePO.setSendmode(2);
		messageTemplateList.add(messageTemplatePO);

		SenderUtil.sendMessageCreateCache(engineerVO, messageTemplateList);
	}

	@After
	public void af() throws Exception {
		Thread.sleep(1000 * 20);
	}
}
